package test
{
	import away3d.containers.View3D;
	import away3d.entities.Mesh;
	import away3d.events.AssetEvent;
	import away3d.events.LoaderEvent;
	import away3d.library.AssetLibrary;
	import away3d.loaders.parsers.AWD2Parser;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	[SWF(width="1280", height="720", backgroundColor="0x000000")]
	
	public class SimpleModel extends Sprite
	{
		//engine variables
		private var view:View3D;
		private var MESH_URL:String = "../asset/max/test/test.awd";
//		private var TEXTURE_URL:String = "onkba_N.jpg";
		private var assetsThatAreloaded:int = 0;
		private var assetsToLoad:int = 2;
		private var model:Mesh;
		
		public function SimpleModel()
		{
			super();
			initEngine();
			initLoading();
		}
		
		private function initEngine():void
		{
			view = new DebugView();
			addChild(view);
			
//			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		/**
		 * Initialise the scene objects
		 */
		private function initLoading():void
		{
			AssetLibrary.enableParser(AWD2Parser);
			AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete);
			AssetLibrary.addEventListener(LoaderEvent.RESOURCE_COMPLETE, onResourceComplete);
			AssetLibrary.addEventListener(LoaderEvent.LOAD_ERROR, onLoadError);
//			AssetLibrary.load(new URLRequest(TEXTURE_URL));
			AssetLibrary.load(new URLRequest(MESH_URL));
		}
		
		protected function onLoadError(event:LoaderEvent):void
		{
			trace("Error loading: " + event.url);
		}
		
		/**
		 * Listener function for asset complete event on loader
		 */
		private function onAssetComplete(event:AssetEvent):void
		{
			// To not see these names output in the console, comment the
			// line below with two slash'es, just as you see on this line
			trace("Loaded " + event.asset.name + " Name: " + event.asset.name);
		}
		
		private function onResourceComplete(ev:LoaderEvent):void
		{
//			assetsThatAreloaded++;
			// check to see if we have all we need
//			if (assetsThatAreloaded == assetsToLoad) {
			setupScene();
//			}
//			view.render();
		}
		
		
		private function setupScene():void {
			model = Mesh(AssetLibrary.getAsset("Plane001"));
			view.scene.addChild(model);
//			var box:Mesh = Mesh(AssetLibrary.getAsset("Box001"));
//			view.scene.addChild(box);
			var model:Mesh = Mesh(AssetLibrary.getAsset("model"));
			view.scene.addChild(model);
		}
		
		private function onEnterFrame(event:Event):void
		{
			view.render();
//			trace(hoverController.panAngle+":"+hoverController.tiltAngle);
//			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
	}
}